package com.dongdongkeji.base.common;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import icepick.Icepick;
import io.reactivex.disposables.CompositeDisposable;

/**
 * Author: ChenJing
 * Date: 2017-06-19 下午 6:17
 * Version: 1.0
 */
public abstract class BaseActivity extends AppCompatActivity{

    private Unbinder unbinder;
    protected CompositeDisposable disposables;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(appointLayoutId());

        disposables = new CompositeDisposable();

        unbinder = ButterKnife.bind(this);

        ActivityManager.getInstance().addActivity(this);

        /*
          状态保存， 子类需要保存状态的全局变量须加入@State注解
         */
        Icepick.restoreInstanceState(this, savedInstanceState);

        initView();
        setListener();
        initRxEvent();
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
        View contentView = getWindow().getDecorView().findViewById(android.R.id.content);
        ActivityManager.unbindReferences(contentView);
        ActivityManager.getInstance().removeActivity(this);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Icepick.saveInstanceState(this, outState);
    }

    protected void initView(){}

    protected void setListener(){}

    protected void initRxEvent(){}

    protected void getData(){}

    protected abstract int appointLayoutId();
}
